<?php
@header('Content-Type: text/html; charset=UTF-8');
define('CURSCRIPT', 'index');
$s = microtime();
error_reporting(E_ALL);
require './source/class/class_core.php';
$mainframe = & cmsCore::instance();
$mainframe->init();


require_once libfile('class/wxrParser');
$result = wxrParser::parse('wordpress.xml');
$post = array();
$category = $tags = array();
foreach((array)$result['posts'] as $k=>$v){
	$p = $v['post_type'];
	$post[$p][] = $v;
}
if($result['categories']){
	$catrow = array(
			'upid'=>0,
			'catname' => '',
			'allowcomment' => 1,
			'closed' => 0,
			'primaltplname' => 'portal/list',
			'contenttplname' => 'portal/view',
			'foldername' => ''
			);
	foreach((array)$result['categories'] as $k=>$v){
		$upid = 0;
		$catname = $v['cat_name'];
		$sql = "SELECT catid FROM #__common_category WHERE catname='$catname'";
		$catid = DB::result_first($sql);
		if(!$catid){
			$catrow['catname'] = $catname;
			if($v['category_parent']){
				if(isset($category[$v['category_parent']])){
					$upid = $category[$v['category_parent']]['catid'];
				}
			}
			$catrow['upid'] = $upid;
			$catid = DB::insert('common_category', $catrow, 1);
		}
		$v['upid'] = $upid;
		$v['catid'] = $catid;
		$category[$v['category_nicename']] = $v;
	}
	require_once libfile('function/cache');
	updatecache('portalcategory');
	unset($result['categories']);
}
if($result['tags']){
	foreach((array)$result['tags'] as $k=>$v){
		$tagname = $v['tag_name'];
		$sql = "SELECT tagid FROM #__common_tagslist WHERE tagname='$tagname'";
		$tagid = DB::result_first($sql);
		if(!$tagid){
			$tagid = DB::insert('common_tagslist', array('tagname'=>$tagname), 1);
		}
		$v['tagid'] = $tagid;
		$tags[$v['tag_slug']] = $v;
	}
	unset($result['tags']);
}
$posts = array();
if($post['post']){
	foreach((array)$post['post'] as $k=>$v){
		$article = processWP($v, $tags, $category);
		$posts[$v['post_id']] = $article;
	}
	unset($post['post']);
}
if($post['attachment']){
	foreach((array)$post['attachment'] as $k=>$v){
		pre($posts[$v['post_parent']]);
		pre($v, 1);
	}
}
unset($post['page']);
unset($post['nav_menu_item']);

pre($post);
exit;



$s = microtime(true);
echo '开始加载splitword<br>';
require libfile('class/splitword');
echo '加载文件：';echo microtime(true) - $s;echo '<br>';
$sp = new SplitWord();
echo '初始化类：';echo microtime(true) - $s;echo '<br>';
$str = '南京市长江大桥';
$sp->SetResultType(2);
$sp->SetSource($str);
echo '设置字符串：';echo microtime(true) - $s;echo '<br>';
$sp->StartAnalysis();
echo '分析字符串：';echo microtime(true) - $s;echo '<br>';
$titleindexs = $sp->GetFinallyIndex(20, ',');
echo '获取调频词：';echo microtime(true) - $s;echo '<br>';

echo $sp->GetFinallyResult().'<br>';
echo '获取分词结果：';echo microtime(true) - $s;echo '<br>';
pre($titleindexs);
echo '内存占用：';echo memory_get_usage();echo '<br>';
unset($sp);
echo '内存占用：';echo memory_get_usage();echo '<br>';
echo microtime(true) - $s;echo '<br>';
require libfile('class/chinese');
echo microtime(true) - $s;echo '<br>';
$ch = new Chinese('UTF-8', 'PINYIN');
echo '开始Chinese PY:<br>';
$s = microtime(true);
$ch->type = 1;
echo '<pre>';echo $ch->Convert($str);echo '</pre>';
echo '<br>Chinese PY 共耗费 :';
echo microtime(true) - $s;echo '<br>';
echo '内存占用：';echo memory_get_usage();echo '<br>';

require_once libfile('class/wxrParser');
$result = wxrParser::parse('wordpress.xml');
pre($result);exit;


require_once libfile('class/xml');
$content = file_get_contents('wordpress.xml');
//$content = preg_replace('@<([a-z_]+):([a-z_]+)>@i', '<\\1_\\2>', $content);
//$content = preg_replace('@</([a-z_]+):([a-z_]+)>@i', '</\\1_\\2>', $content);
echo code($content);
//exit;
$rss = new SimplePie();
$rss->set_raw_data($content);
$rss->init();
$data = $rss->data;
pre($data);
$data = current($rss->data['child']);
$data = current($data['rss'][0]['child']);
$data = current($data['channel'][0]['child']);
//unset($data['data']);
echo count($data);
//$data = $data['item'];
//pre($data);
foreach((array)$data as $k=>$v) $data[$k] = processwp($v);
//pre($data);
$items = $data['item'];
$a = array('link', 'pubDate', 'guid', 'wp_post_date', 'wp_ping_status', 'wp_postmeta');
$post = $attachs = $page = array();
foreach((array)$items as $k=>$v){
	foreach((array)$a as $vv){
		if(isset($v[$vv])) unset($v[$vv]);
	}
	$items[$k] = $v;
	if($v['wp_status']=='trash') unset($items[$k]);
	elseif($v['wp_post_type']=='nav_menu_item') unset($items[$k]);
	if($v['wp_post_type'] == 'attachment') {
		$attachs[] = $v;
		unset($items[$k]);
	}
	if($v['wp_post_type'] == 'post') {
		$key = $v['wp_post_id'];
		if(isset($v['category'])){
			if(isset($v['category']['data'])){
				$v['category'] = $v['category']['data'];
			} else {
				if(is_array($v['category'])){
					$var = $v['category'];
					foreach((array)$var as $vv){
						if($vv['attribs']['domain']=='category'){
							$v['category'] = $vv['data'];
						} else {
							$kk = $vv['attribs']['domain'];
							$v[$kk][] = $vv['data'];
						}
					}
				}
			}
		}
		if(isset($pos[$key])){
			echo 'repeat<br>';
			pre($v);
			pre($post[$key]);
			exit;
		} else {
			$post[$key] = $v;
		}
		unset($items[$k]);
	}
	if($v['wp_post_type'] == 'page') {
		$page[] = $v;
		unset($items[$k]);
	}
}
pre($items);
pre($attachs);
function processwpxml($array = array()){
	if(count($array)==1 && is_array($array)) $array = current($array);
	if(isset($array['attribs']) && $array['attribs'] && isset($array['data']) && !$array['data']){
		$array['data'] = $array['attribs'];
		if(is_array($array) && isset($array['attribs']))
		unset($array['attribs']);
	}
	if(is_array($array) && isset($array['xml_base']))
	unset($array['xml_base']);
	if(is_array($array) && isset($array['xml_base_explicit']))
	unset($array['xml_base_explicit']);
	if(is_array($array) && isset($array['xml_lang']))
	unset($array['xml_lang']);
	if(count($array)==1 && is_array($array)) $array = current($array);
	if(isset($array['child']) && is_array($array['child']) && count($array['child'])==1){
		$v = $array['child'];
		$v = current($v);
		$array['child'] = $v;
	}
	if(isset($array['child']) && is_array($array['child'])){
		foreach((array)$array['child'] as $k=>$v){
			if(is_array($v))
			foreach((array)$v as $key=>$var){
				$v[$key] = processwp($var);
			}
			if(is_array($v) && count($v)==1) $v=current($v);
			$array['child'][$k] = $v;
		}
	}
	if(is_array($array)){
		foreach((array)$array as $key=>$var){
			if(!$var) unset($array[$key]);
			else
			$array[$key] = processwp($var);
		}
	}
	if(is_array($array) && isset($array['child'])) {
		if(!trim($array['data'])){
			$array['data'] = $array['child'];
			unset($array['child']);
			if(count($array)==1) $array = $array['data'];
		}
	}
	if(is_array($array) && count($array)==1){
		foreach((array)$array as $k=>$v) $array = $v;
		//$array = current($array);
	}
	return $array;
}

